package cn.yikaojiuguo.student.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.support.utils.ViewUtils;
import cn.yikaojiuguo.student.ui.view.TouchImageView;
import de.greenrobot.event.EventBus;

/**
 * Created by cfan_yang on 2016/1/18.
 */
public class ImgsAdapter extends PagerAdapter {

    public List<String> mList;
    private Context mContext;
    private int mChildCount = 0;

    public ImgsAdapter(Context context, List<String> list) {
        this.mList = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View contentView = View.inflate(mContext, R.layout.item_imgs_adpter, null);
        final TouchImageView tiv = ViewUtils.find(contentView, R.id.tiv);
        final ProgressBar pb = ViewUtils.find(contentView,R.id.pb);
        tiv.setVisibility(View.GONE);
        pb.setVisibility(View.VISIBLE);
        String uri = mList.get(position);
        if(uri.contains("http")) {
            ImageLoader.getInstance().displayImage(uri, tiv, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    LogUtil.showInfoLog("ImageLoader","image load failed because " + failReason.toString());
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    tiv.setVisibility(View.VISIBLE);
                    tiv.setImageBitmap(loadedImage);
                    pb.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });
        } else {
            LogUtil.showInfoLog("file","path = " + mList.get(position));
            ImageSize imageSize = new ImageSize(480,800);
            ImageLoader.getInstance().loadImage("file:///" + mList.get(position),imageSize, new ImageLoadingListener() {

                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    LogUtil.showInfoLog("ImageLoader","image load failed because " + failReason.toString());
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    LogUtil.showInfoLog("bitmap size","width = " + loadedImage.getWidth() + "height = " + loadedImage.getHeight());
                    tiv.setVisibility(View.VISIBLE);
                    tiv.setImageBitmap(loadedImage);
                    pb.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });
        }
        tiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType
                        .TOP_SHOW_OR_HIDE));
            }
        });
        container.addView(contentView);
        return contentView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}
